<template>
  <!-- 面包屑导航区 -->
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <el-row type="flex" justify="" :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryinfo.keyword"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getuser()"
            ></el-button>
          </el-input>
        </el-col>

        <el-col :span="8">
          <el-button type="primary" @click="showadd()">添加按钮</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="userlist" stripe>
        <el-table-column type="index" label="编号"></el-table-column>
        <el-table-column prop="username" label="用户名" width="180">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180">
        </el-table-column>
        <el-table-column prop="nickname" label="昵称"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 用作用域插槽 查询id -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="showedit(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="showdele(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        background
        :page-sizes="[2, 3, 5, 10]"
        :page-size="queryinfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 修改用户对话框 -->
      <el-dialog
        title="修改用户"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <!-- 修改表单 -->
        <el-form :model="editdata" ref="ruleForm" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input disabled v-model="editdata.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editdata.email"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="editdata.nickname"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 添加用户信息 -->

      <el-dialog
        title="添加用户"
        :visible.sync="dialogVisibleadd"
        width="30%"
        :before-close="handleClose"
      >
        <!-- 添加表单 -->
        <el-form :model="addData" ref="ruleForm" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addData.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="addData.email"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="addData.nickname"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleadd = false">取 消</el-button>
          <el-button type="primary" @click="add()">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 删除对话框 -->
      <el-dialog
        title="删除用户"
        :visible.sync="dialogVisibledele"
        width="30%"
        :before-close="handleClose"
      >
        <span>确定要删除用户</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibledele = false">取 消</el-button>
          <el-button type="primary" @click="dele()">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      queryinfo: {
        page: 1,
        keyword: "",
        //每页显示条目数
        pageSize: 2,
      },
      editdata: {
        id: "",
        username: "",
        nickname: "",
        email: "",
      },
      addData: {
        username: "",
        nickname: "",
        email: "",
      },
      userlist: [],
      //总条目数
      total: 0,
      dialogVisible: false,
      dialogVisibleadd: false,
      dialogVisibledele: false,
    };
  },
  methods: {
    getuser() {
      //获取用户列表
      axios
        .get("http://adminapi.pyg.com/admin/index", { params: this.queryinfo })
        .then((res) => {
          //   console.log(res.data);
          if (res.data.code !== 200 || res.data.msg != "success") {
            return this.$message.error("获取用户列表失败");
          }
          // console.log(res);
          var { data } = res.data;
          // console.log(data);
          this.userlist = data.data;
          //总条目数
          this.total = data.total;
        });
    },

    // 监听pagesize改变时间
    handleSizeChange(newsize) {
      // console.log(newsize);
      this.queryinfo.pageSize = newsize;
      this.getuser();
    },

    //  监听当前页面改变事件
    handleCurrentChange(newPage) {
      this.queryinfo.page = newPage;
      this.getuser();
    },

    // 修改用户
    showedit(row) {
      // console.log(row);
      this.editdata = row;
      this.dialogVisible = true;
    },
    // 添加用户
    showadd() {
      this.dialogVisibleadd = true;
    },
    // 删除用户
    showdele(row) {
      this.dialogVisibledele = true;
      this.userid = row.id;
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 保存用户修改信息并提交
    edit() {
      // 表单验证 邮箱 昵称
      // 发起修改信息的数据请求
      axios.post("http://adminapi.pyg.com/edit", this.editdata).then((res) => {
        // console.log(res);
        if (res.data.code == 200 && res.data.msg == "success") {
          //成功
          this.$message({
            message: "修改成功",
            type: "success",
          });
        } else {
          this.$message.error("修改失败");
        }
      });
      this.dialogVisible = false;
    },
    // 删除
    dele() {
      //  console.log("object");
      //  console.log(this.userid);
      axios
        .get("http://adminapi.pyg.com/dele", { params: { id: this.userid } })
        .then((res) => {
          // console.log(res);
          this.getuser();
          this.dialogVisibledele = false;
        });
    },
    // 添加用户信息
    add() {
      //  console.log("sss");
      axios.post("http://adminapi.pyg.com/add", this.addData).then((res) => {
        // console.log(res);
        if (res.data.code == 200 && res.data.msg == "success") {
          //成功
          this.$message({
            message: "添加成功",
            type: "success",
          });
        } else {
          this.$message.error("添加失败");
        }
        this.dialogVisibleadd = false;
        this.getuser();
      });
    },
  },
  mounted() {
    this.getuser();
  },
};
</script>
<style scoped>
.el-card {
  margin-top: 20px;
  box-shadow: 5 5 0 0.5;
}
.el-table {
  margin-top: 15px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
